package com.changgou.page.service.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.GoodsFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0
 * @ProjectName: changgou_parent
 * @ClassName: PageServiceImpl
 * @Description: TODO(一句话描述该类的功能)
 * @Author: Xu Fj
 * @Date: 2020/3/3 22:45
 */

@Service
public class PageServiceImpl implements PageService {
    @Autowired
    private GoodsFeign goodsFeign;

    @Value("${pagepath}")
    private String pagepath;

    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public void generateHtml(String spuId) {
        //获取context对象,用于存放商品详情页的相关数据
        Context context = new Context();
        //调用方法获取商品相关数据
        Map<String, Object> itemData = this.findItemData(spuId);
        //将数据存入context
        context.setVariables(itemData);

        File dir = new File(pagepath);
        if (!dir.exists()){
            dir.mkdirs();
        }
        //创建一个输出流
        File file = new File(dir + "/" + spuId + ".html");
        Writer out = null;

        try {
            out = new PrintWriter(file);
            templateEngine.process("item",context,out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @Description: TODO(获取静态化页面的相关数据)
     */
    private Map<String, Object> findItemData(String spuId) {
        HashMap<String, Object> resultMap = new HashMap<>();

        //获取spu
        Spu spu = goodsFeign.findSpuById(spuId).getData();
        resultMap.put("spu",spu);

        //获取图片信息
        if (spu != null){
            if (StringUtils.isNotEmpty(spu.getImages())){
                resultMap.put("imageList",spu.getImages().split(","));
            }
        }

        //获取分类信息
        Category category1 = goodsFeign.findById(spu.getCategory1Id()).getData();
        resultMap.put("category1",category1);

        Category category2 = goodsFeign.findById(spu.getCategory2Id()).getData();
        resultMap.put("category2",category2);

        Category category3 = goodsFeign.findById(spu.getCategory3Id()).getData();
        resultMap.put("category3",category3);

        //获取sku相关数据信息
        List<Sku> skuList = goodsFeign.findListBySpuId(spuId);
        resultMap.put("skuList",skuList);

        //获取商品规格信息
        Map specificationList = JSON.parseObject(spu.getSpecItems(), Map.class);
        resultMap.put("specificationList",specificationList);
        return resultMap;

    }
}
